package com.lframework.xingyun.promo.impl.member;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lframework.starter.web.core.impl.BaseMpServiceImpl;
import com.lframework.starter.web.core.utils.PageResultUtil;
import com.lframework.starter.web.core.components.resp.PageResult;
import java.io.Serializable;
import com.lframework.starter.common.utils.Assert;
import com.lframework.xingyun.promo.entity.MarketingMemberLevel;
import com.lframework.xingyun.promo.mappers.MarketingMemberLevelMapper;
import com.lframework.xingyun.promo.service.member.MarketingMemberLevelService;
import com.lframework.xingyun.promo.vo.member.QueryMarketingMemberLevelVo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class MarketingMemberLevelServiceImpl extends BaseMpServiceImpl<MarketingMemberLevelMapper, MarketingMemberLevel> implements MarketingMemberLevelService {

    @Override
    public PageResult<MarketingMemberLevel> queryPage(Integer pageIndex, Integer pageSize, QueryMarketingMemberLevelVo vo) {

        Assert.greaterThanZero(pageIndex);
        Assert.greaterThanZero(pageSize);

        IPage<MarketingMemberLevel> page = this.getBaseMapper().queryPage(vo);

        return PageResultUtil.convert(page);
    }

    @Override
    public List<MarketingMemberLevel> queryList(QueryMarketingMemberLevelVo vo) {

        return getBaseMapper().queryList(vo);
    }

    @Cacheable(value = MarketingMemberLevel.CACHE_NAME, key = "@cacheVariables.tenantId() + #id", unless = "#result == null")
    @Override
    public MarketingMemberLevel findById(String id) {

        return getBaseMapper().selectById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void batchAdd(String marketingMemberId, List<String> memberLevelIds) {
        // 新增新的关系
        List<MarketingMemberLevel> marketingMemberLevels = new ArrayList<>();
        for (String memberLevelId : memberLevelIds) {
            MarketingMemberLevel marketingMemberLevel = new MarketingMemberLevel();
            marketingMemberLevel.setMarketingMembId(marketingMemberId);
            marketingMemberLevel.setMemberLevelId(memberLevelId);
            marketingMemberLevels.add(marketingMemberLevel);
        }
        getThis(MarketingMemberLevelService.class).saveBatch(marketingMemberLevels);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void batchUpdate(String marketingMemberId, List<String> memberLevelIds) {
        // 删除旧的关系
        getThis(MarketingMemberLevelService.class).remove(Wrappers.lambdaUpdate(MarketingMemberLevel.class)
                .eq(MarketingMemberLevel::getMarketingMembId, marketingMemberId));
        getThis(MarketingMemberLevelService.class).batchAdd(marketingMemberId,  memberLevelIds);
    }

    @CacheEvict(value = MarketingMemberLevel.CACHE_NAME, key = "@cacheVariables.tenantId() + #key")
    @Override
    public void cleanCacheByKey(Serializable key) {

    }
}

